home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2004 July / APC0407D2.iso / workshop / onlineco / files / ImageMagick-6.0.1-Q16-windows-dll.exe / {app} / PerlMagick / demo / Turtle.pm < prev    next >
Encoding:
Perl POD Document  |  2003-03-25  |  927 b   |  57 lines

  1. package Turtle;
  2.  
  3. # Written by jreed@itis.com, adapted by John Cristy.
  4.  
  5. sub new
  6. {
  7.   my $class = shift;
  8.   my $self = {};
  9.  
  10.   @{$self}{qw(x y theta mirror)} = @_;
  11.   bless $self, $class;
  12. }
  13.  
  14. sub forward
  15. {
  16.   my $self = shift;
  17.   my ($r, $what) = @_;
  18.   my ($newx, $newy)=($self->{x}+$r* sin($self->{theta}),
  19.                      $self->{y}+$r*-cos($self->{theta}));
  20.   if ($what) {
  21.     &$what($self->{x}, $self->{y}, $newx, $newy);  # motion
  22.   }
  23.   # According to the coderef passed in
  24.   ($self->{x}, $self->{y})=($newx, $newy);  # change the old coords
  25. }
  26.  
  27. sub turn
  28. {
  29.   my $self = shift;
  30.   my $dtheta = shift;
  31.  
  32.   $self->{theta} += $dtheta*$self->{mirror};
  33. }
  34.  
  35. sub state
  36. {
  37.   my $self = shift;
  38.  
  39.   @{$self}{qw(x y theta mirror)};
  40. }
  41.  
  42. sub setstate
  43. {
  44.   my $self = shift;
  45.  
  46.   @{$self}{qw(x y theta mirror)} = @_;
  47. }
  48.  
  49. sub mirror
  50. {
  51.   my $self = shift;
  52.  
  53.   $self->{mirror} *= -1;
  54. }
  55.  
  56. "Turtle.pm";
  57.